<html>
<head><meta charset="utf-8"><title>explicit reborrows · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/explicit.20reborrows.html">explicit reborrows</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="211518919"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/explicit%20reborrows/near/211518919" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/explicit.20reborrows.html#211518919">(Sep 28 2020 at 16:54)</a>:</h4>
<p>Does someone know why I have to explicitly reborrow here?</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">walk_abstract_const</span><span class="o">&lt;'</span><span class="na">tcx</span><span class="p">,</span><span class="w"> </span><span class="n">F</span><span class="o">&gt;</span><span class="p">(</span><span class="n">tcx</span>: <span class="nc">TyCtxt</span><span class="o">&lt;'</span><span class="na">tcx</span><span class="o">&gt;</span><span class="p">,</span><span class="w"> </span><span class="n">ct</span>: <span class="nc">AbstractConst</span><span class="o">&lt;'</span><span class="na">tcx</span><span class="o">&gt;</span><span class="p">,</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">f</span>: <span class="nc">F</span><span class="p">)</span><span class="w"></span>
<span class="k">where</span><span class="w"></span>
<span class="w">    </span><span class="n">F</span>: <span class="nb">FnMut</span><span class="p">(</span><span class="n">Node</span><span class="o">&lt;'</span><span class="na">tcx</span><span class="o">&gt;</span><span class="p">),</span><span class="w"></span>
<span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">recurse</span><span class="p">(</span><span class="n">tcx</span><span class="p">,</span><span class="w"> </span><span class="n">ct</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">f</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">recurse</span><span class="o">&lt;'</span><span class="na">tcx</span><span class="o">&gt;</span><span class="p">(</span><span class="n">tcx</span>: <span class="nc">TyCtxt</span><span class="o">&lt;'</span><span class="na">tcx</span><span class="o">&gt;</span><span class="p">,</span><span class="w"> </span><span class="n">ct</span>: <span class="nc">AbstractConst</span><span class="o">&lt;'</span><span class="na">tcx</span><span class="o">&gt;</span><span class="p">,</span><span class="w"> </span><span class="n">f</span>: <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="k">dyn</span><span class="w"> </span><span class="nb">FnMut</span><span class="p">(</span><span class="n">Node</span><span class="o">&lt;'</span><span class="na">tcx</span><span class="o">&gt;</span><span class="p">))</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">root</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ct</span><span class="p">.</span><span class="n">root</span><span class="p">();</span><span class="w"></span>
<span class="w">        </span><span class="n">f</span><span class="p">(</span><span class="n">root</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="k">match</span><span class="w"> </span><span class="n">root</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="n">Node</span>::<span class="n">Leaf</span><span class="p">(</span><span class="n">_</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">(),</span><span class="w"></span>
<span class="w">            </span><span class="n">Node</span>::<span class="n">Binop</span><span class="p">(</span><span class="n">_</span><span class="p">,</span><span class="w"> </span><span class="n">l</span><span class="p">,</span><span class="w"> </span><span class="n">r</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="n">walk_abstract_const</span><span class="p">(</span><span class="n">tcx</span><span class="p">,</span><span class="w"> </span><span class="n">ct</span><span class="p">.</span><span class="n">subtree</span><span class="p">(</span><span class="n">l</span><span class="p">),</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="o">*</span><span class="n">f</span><span class="p">);</span><span class="w"></span>
<span class="w">                </span><span class="n">walk_abstract_const</span><span class="p">(</span><span class="n">tcx</span><span class="p">,</span><span class="w"> </span><span class="n">ct</span><span class="p">.</span><span class="n">subtree</span><span class="p">(</span><span class="n">r</span><span class="p">),</span><span class="w"> </span><span class="n">f</span><span class="p">);</span><span class="w"></span>
<span class="w">            </span><span class="p">}</span><span class="w"></span>
<span class="w">            </span><span class="n">Node</span>::<span class="n">UnaryOp</span><span class="p">(</span><span class="n">_</span><span class="p">,</span><span class="w"> </span><span class="n">v</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="n">walk_abstract_const</span><span class="p">(</span><span class="n">tcx</span><span class="p">,</span><span class="w"> </span><span class="n">ct</span><span class="p">.</span><span class="n">subtree</span><span class="p">(</span><span class="n">v</span><span class="p">),</span><span class="w"> </span><span class="n">f</span><span class="p">);</span><span class="w"></span>
<span class="w">            </span><span class="p">}</span><span class="w"></span>
<span class="w">            </span><span class="n">Node</span>::<span class="n">FunctionCall</span><span class="p">(</span><span class="n">func</span><span class="p">,</span><span class="w"> </span><span class="n">args</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="n">walk_abstract_const</span><span class="p">(</span><span class="n">tcx</span><span class="p">,</span><span class="w"> </span><span class="n">ct</span><span class="p">.</span><span class="n">subtree</span><span class="p">(</span><span class="n">func</span><span class="p">),</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="o">*</span><span class="n">f</span><span class="p">);</span><span class="w"></span>
<span class="w">                </span><span class="k">for</span><span class="w"> </span><span class="o">&amp;</span><span class="n">arg</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">args</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                    </span><span class="n">walk_abstract_const</span><span class="p">(</span><span class="n">tcx</span><span class="p">,</span><span class="w"> </span><span class="n">ct</span><span class="p">.</span><span class="n">subtree</span><span class="p">(</span><span class="n">arg</span><span class="p">),</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="o">*</span><span class="n">f</span><span class="p">);</span><span class="w"></span>
<span class="w">                </span><span class="p">}</span><span class="w"></span>
<span class="w">            </span><span class="p">}</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>Not doing so results in "error[E0382]: borrow of moved value: <code>f</code>". Iirc we automatically reborrow implicitly in some cases, so I am surprised that this isn't happening here</p>



<a name="211519039"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/explicit%20reborrows/near/211519039" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/explicit.20reborrows.html#211519039">(Sep 28 2020 at 16:55)</a>:</h4>
<p>I ran into this issue with sqlx, apparently <code>F</code> being a trait means the compiler doesn't know to reborrow?</p>



<a name="211519146"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/explicit%20reborrows/near/211519146" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/explicit.20reborrows.html#211519146">(Sep 28 2020 at 16:56)</a>:</h4>
<p>So the issue is that <code>f</code> is a trait object?</p>



<a name="211519161"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/explicit%20reborrows/near/211519161" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/explicit.20reborrows.html#211519161">(Sep 28 2020 at 16:56)</a>:</h4>
<p>I think so</p>



<a name="211519182"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/explicit%20reborrows/near/211519182" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/explicit.20reborrows.html#211519182">(Sep 28 2020 at 16:56)</a>:</h4>
<p>wait, I just realized that this code is wrong</p>



<a name="211519188"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/explicit%20reborrows/near/211519188" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/explicit.20reborrows.html#211519188">(Sep 28 2020 at 16:56)</a>:</h4>
<p>uff</p>



<a name="211519195"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/explicit%20reborrows/near/211519195" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/explicit.20reborrows.html#211519195">(Sep 28 2020 at 16:56)</a>:</h4>
<p>well no - that <code>walk_abstract_const</code> <em>takes</em> a generic type implementing a trait</p>



<a name="211519243"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/explicit%20reborrows/near/211519243" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/explicit.20reborrows.html#211519243">(Sep 28 2020 at 16:57)</a>:</h4>
<p><code>recurse</code> has to call <code>recurse</code> itself instead of <code>walk_abstract_const</code></p>



<a name="211519246"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/explicit%20reborrows/near/211519246" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/explicit.20reborrows.html#211519246">(Sep 28 2020 at 16:57)</a>:</h4>
<p>you'd have the same issue calling <code>walk_abstract_const</code> with a concrete type</p>



<a name="211519309"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/explicit%20reborrows/near/211519309" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/explicit.20reborrows.html#211519309">(Sep 28 2020 at 16:57)</a>:</h4>
<p>aaah, and now the reborrow happens implicitly</p>



<a name="211519356"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/explicit%20reborrows/near/211519356" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/explicit.20reborrows.html#211519356">(Sep 28 2020 at 16:58)</a>:</h4>
<p>Thank you <span aria-label="laughing" class="emoji emoji-1f606" role="img" title="laughing">:laughing:</span> <span aria-label="heart" class="emoji emoji-2764" role="img" title="heart">:heart:</span></p>



<a name="211519416"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/explicit%20reborrows/near/211519416" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/explicit.20reborrows.html#211519416">(Sep 28 2020 at 16:58)</a>:</h4>
<p>no problem :) thank @mehcode, they explained it to me the first time</p>



<a name="211522159"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/explicit%20reborrows/near/211522159" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/explicit.20reborrows.html#211522159">(Sep 28 2020 at 17:18)</a>:</h4>
<p>See also <a href="https://stackoverflow.com/q/48143889/155423">Why can't I reuse a &amp;mut reference after passing it to a function that accepts a generic type?</a></p>



<a name="211532649"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/explicit%20reborrows/near/211532649" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dániel Buga <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/explicit.20reborrows.html#211532649">(Sep 28 2020 at 18:45)</a>:</h4>
<p>I would've loved an explanation in that SO answer on _why_ there's no implicit reborrow made by the compiler there</p>



<a name="211532831"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/explicit%20reborrows/near/211532831" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/explicit.20reborrows.html#211532831">(Sep 28 2020 at 18:46)</a>:</h4>
<p>I can add it if you find it!</p>



<a name="211532845"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/explicit%20reborrows/near/211532845" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/explicit.20reborrows.html#211532845">(Sep 28 2020 at 18:46)</a>:</h4>
<p>because in the current compiler architecture, you only get a chance to do a coercion if both source and target type are known at the coercion site</p>



<a name="211532990"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/explicit%20reborrows/near/211532990" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/explicit.20reborrows.html#211532990">(Sep 28 2020 at 18:47)</a>:</h4>
<p><span class="user-mention" data-user-id="211727">@Jonas Schievink</span> you feel confident enough about that that I should edit it in?</p>



<a name="211533140"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/explicit%20reborrows/near/211533140" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/explicit.20reborrows.html#211533140">(Sep 28 2020 at 18:48)</a>:</h4>
<p>I think so, yeah</p>



<a name="211533731"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/explicit%20reborrows/near/211533731" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/explicit.20reborrows.html#211533731">(Sep 28 2020 at 18:52)</a>:</h4>
<p>Added.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>